home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / faq-s.zip / SCRNINPT.PAS < prev    next >
Pascal/Delphi Source File  |  1990-06-02  |  4KB  |  198 lines

  1. unit scrninpt;
  2.  
  3. { Makes it so READLN uses a nice little scrollable region }
  4. { The default input region is the cursor position to the end of the line }
  5.  
  6. {$R-,S-,I-,D-,V-,B-,L+}
  7.  
  8. interface
  9.  
  10. uses dos,crt,
  11.      scrnunit;
  12.  
  13. var scrnin:text;         { For input }
  14.     buflen:integer;
  15.  
  16.  
  17. procedure setinputregion (left,right,line:integer);
  18. procedure setdefaultinput (x:string);
  19. procedure setinputcolor (attr:integer);
  20.  
  21. implementation
  22.  
  23. var scrninbuf:array [0..257] of char;
  24.     x1,x2,y:integer;
  25.     oldinput:string;
  26.  
  27. {$F+}
  28.  
  29. function donothing (var t:textrec):integer;
  30. begin
  31. {  t.bufend:=0;
  32.   t.bufpos:=0;  }
  33.   donothing:=0
  34. end;
  35.  
  36. function scrninchars (var t:textrec):integer;
  37. var s:string;
  38.     len:byte absolute s;
  39.     cx,lx,wid:integer;
  40.     k:char;
  41.     tracking:boolean;
  42.  
  43. const letters:set of char=['A'..'Z','a'..'z'];
  44.  
  45.   procedure drawit;
  46.   var cnt:integer;
  47.   begin
  48.     gotoxy (x1,y);
  49.     write (scrn,copy(s,lx,wid));
  50.     for cnt:=1 to wid-len+lx-1 do write (' ');
  51.     gotoxy (cx-lx+x1,y);
  52.     movecsr
  53.   end;
  54.  
  55.   procedure insert (k:char);
  56.   begin
  57.     if len>=buflen then exit;
  58.     s:=copy(s,1,cx-1)+k+copy(s,cx,255);
  59.     cx:=cx+1
  60.   end;
  61.  
  62.   procedure del;
  63.   begin
  64.     if cx<=len then s:=copy(s,1,cx-1)+copy(s,cx+1,255)
  65.   end;
  66.  
  67.   procedure backspace;
  68.   begin
  69.     if cx>1 then begin
  70.       cx:=cx-1;
  71.       del
  72.     end
  73.   end;
  74.  
  75.   procedure wordleft;
  76.   begin
  77.     if cx=1 then exit;
  78.     cx:=cx-1;
  79.     while (cx>1) and ((s[cx-1] in letters) or (not (s[cx] in letters))) do
  80.       cx:=cx-1
  81.   end;
  82.  
  83.   procedure wordright;
  84.   begin
  85.     if cx>len then exit;
  86.     cx:=cx+1;
  87.     while (cx<=len) and ((s[cx-1] in letters) or (not (s[cx] in letters))) do
  88.       cx:=cx+1;
  89.   end;
  90.  
  91.   procedure delword;
  92.   begin
  93.     while (cx<=len) and (s[cx] in letters) do del;
  94.     while (cx<=len) and (not (s[cx] in letters)) do del
  95.   end;
  96.  
  97.   procedure extended (key:integer);
  98.   begin
  99.     case key of
  100.       71,73:cx:=1;
  101.       75:cx:=cx-1;
  102.       77:cx:=cx+1;
  103.       79,81:cx:=len+1;
  104.       83:del;
  105.       115:wordleft;
  106.       116:wordright;
  107.       117:len:=cx-1;
  108.     end
  109.   end;
  110.  
  111.   procedure normal (k:char);
  112.   begin
  113.     case ord(k) of
  114.       32..126:if len<buflen then insert(k);
  115.       128..255:if len<buflen then insert(k); {demo}
  116.       8:backspace;
  117.       27:len:=0;
  118.       127,20:delword
  119.     end
  120.   end;
  121.  
  122. begin
  123.   scrninchars:=0;
  124.   if t.bufend<>t.bufpos then exit;
  125.   pushdarea;
  126.   setcursortracking (false);
  127.   setcolor (curwindowptr^.inputcolor);
  128.   s:=oldinput;
  129.   if x1=0 then begin
  130.     x1:=wherex;
  131.     y:=wherey;
  132.     x2:=curwindowptr^.xsize
  133.   end;
  134.   lx:=1;
  135.   cx:=1;
  136.   wid:=x2-(x1+1);
  137.   repeat
  138.     if cx<1 then cx:=1;
  139.     if cx>len then cx:=len+1;
  140.     if lx>cx-5 then lx:=cx-5;
  141.     if lx<cx-wid+5 then lx:=cx-wid+5;
  142.     if lx>len-wid+1 then lx:=len-wid+1;
  143.     if lx>cx then lx:=cx;
  144.     if lx<cx-wid then lx:=cx-wid;
  145.     if lx<1 then lx:=1;
  146.     if not keypressed then drawit;
  147.     k:=readkey;
  148.     if k=#0 then extended(ord(readkey)) else normal(k)
  149.   until k=#13;
  150.   drawit;
  151.   s:=s+#13#10;
  152.   move (s[1],t.bufptr^,length(s));
  153.   x1:=0;
  154.   buflen:=80;
  155.   oldinput:='';
  156.   t.bufpos:=0;
  157.   t.bufend:=len;
  158.   popdarea
  159. end;
  160.  
  161. {$F+}
  162.  
  163. procedure setinputregion (left,right,line:integer);
  164. begin
  165.   x1:=left;
  166.   x2:=right;
  167.   y:=line
  168. end;
  169.  
  170. procedure setdefaultinput (x:string);
  171. begin
  172.   oldinput:=x
  173. end;
  174.  
  175. procedure setinputcolor (attr:integer);
  176. begin
  177.   curwindowptr^.inputcolor:=attr
  178. end;
  179.  
  180. begin
  181.   x1:=0;          { Initialize input stuff }
  182.   buflen:=80;
  183.   oldinput:='';
  184.   with textrec(scrnin) do begin
  185.     mode:=fminput;
  186.     bufptr:=@scrninbuf;
  187.     bufsize:=258;
  188.     openfunc:=@donothing;
  189.     closefunc:=@donothing;
  190.     inoutfunc:=@scrninchars;
  191.     flushfunc:=@donothing;
  192.     bufpos:=0;
  193.     bufend:=0
  194.   end;
  195.   move (scrnin,input,sizeof(textrec))
  196. end.
  197.  
  198.